<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BaifuController extends Controller
{
    public function Pay()
    {
        $notifyurl = 'http://jhpay.yizhibank.com/baifuback'; //异步通知
        $gateway = 'https://tech.eidpay.com:6443/scanPay';        //提交地址
        $money = 100;
        //交易类型: 070201 微信扫码 070301 支付宝扫码
        $key_file = storage_path('key/999002100009999.pem');			//密钥
        $cert_file = storage_path('key/999002100009999_cacert.pem');   //根证书
        $str = date('YmdHis').'|'.str_pad($money,15,0,STR_PAD_LEFT).'|'.$notifyurl.'|产品介绍|01';
//        dd($str);
	$sha1_str=sha1($str);
        
        $signature = $this->sign($sha1_str);
//        dd($signature);
        $_reqeust = array(
            'merId'=>'999002100009999', 					//商户号
            'tradeCode'=>'070201',  				//微信扫码
//            'tradeCode'=>'070301',  				//支付宝扫码
            'orderId'=>date('YmdHis'). mt_rand(11, 99), 			    //订单号
            'msg'=>$str,
        );
	$date='';
	foreach ($_reqeust as $key => $val) {
            $date = $date . $key . "=" . $val . "&";
        }
        $date=$date.'signature='.$signature;	
//        echo $date;exit;
        $tuCurl = curl_init();   
        curl_setopt($tuCurl, CURLOPT_URL, $gateway);   
        curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);  
        curl_setopt($tuCurl, CURLOPT_HEADER, 0);    
        curl_setopt($tuCurl, CURLOPT_SSLCERT,$key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD,"123pay");  
        curl_setopt($tuCurl, CURLOPT_SSLCERTTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_SSLKEY, $key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLKEYPASSWD,"123pay");  
        curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_CAINFO, $cert_file);         
        curl_setopt($tuCurl, CURLOPT_POST, 1);   
        curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false);   
        curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $date);   
        $tuData = curl_exec($tuCurl);    
        if(!curl_errno($tuCurl)){   
            $info = curl_getinfo($tuCurl);   
//            echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];   
        } else {   
            echo 'Curl error: ' . curl_error($tuCurl);   
        }
        curl_close($tuCurl);   
        dd($tuData);
//        $_returnData = json_decode($tuData,true);
//        if($_returnData['respCode'] == '00000'){
//            
//        }else{
//            ErrorReturn($_returnData['respInfo']);
//        }

    }
    
    public function searchResult4Order(){
        $gateway = 'https://tech.eidpay.com:6443/scanPay';        //提交地址
        
        $key_file = storage_path('key/999002100009999.pem');			//密钥
        $cert_file = storage_path('key/999002100009999_cacert.pem');   //根证书
//        echo $cert_file; exit;
        $str = date('YmdHis').'|'.'2017080809585034'.'|'.'070301';
	$sha1_str=sha1($str);
        
        $signature = $this->sign($sha1_str);

        $_reqeust = array(
            'merId'=>'999002100009999', 					//商户号
            'tradeCode'=>'070101',  				//交易结果查询
            'orderId'=>date('YmdHis'). mt_rand(11, 99), 			    //订单号
            'msg'=>$str,
        );
        $date='';
	foreach ($_reqeust as $key => $val) {
            $date = $date . $key . "=" . $val . "&";
        }
        $date=$date.'signature='.$signature;
//        echo $date;exit;
        $tuCurl = curl_init();   
        curl_setopt($tuCurl, CURLOPT_URL, $gateway);   
        curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);  
        curl_setopt($tuCurl, CURLOPT_HEADER, 0);    
        curl_setopt($tuCurl, CURLOPT_SSLCERT,$key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD,"123pay");
        curl_setopt($tuCurl, CURLOPT_SSLCERTTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_SSLKEY, $key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLKEYPASSWD,"123pay");  
        curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_CAINFO, $cert_file);         
        curl_setopt($tuCurl, CURLOPT_POST, 1);   
        curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false);   
        curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $date);   
        $tuData = curl_exec($tuCurl);    
        if(!curl_errno($tuCurl)){   
            $info = curl_getinfo($tuCurl);   
                //echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];   
        } else {   
                echo 'Curl error: ' . curl_error($tuCurl);   
        }   
        curl_close($tuCurl);   
        echo $tuData;
    }
    
    //单笔代付
    function singlePayment4Baifu(){
        $notifyurl = 'http://jhpay.yizhibank.com/baifuback'; //异步通知
        $gateway = 'http://tech.eidpay.com:8080/juPay';        //提交地址
        $money = 100;
        $key_file = storage_path('key/898000000020186_public.pem');			//密钥
        $bankcard = '6228480038395977176';
        $orderId = date('YmdHis'). mt_rand(11, 99);
        $name = '中宏测试';
        $str = date('YmdHis').'|02|'.$bankcard.'|'.$name.'||||中国建设银行|'.str_pad($money,15,0,STR_PAD_LEFT).'|资金用途';
        $msg = $this->encrypt($str,$key_file,1,'123pay'); //对str进行加密
//	$sha1_str=sha1($str);
        $signature_str='898000000020186'.$orderId.'500302'.$msg; //商户编号+订单号+代号+msg
        $signature = $this->juSignature($signature_str,$key_file);  //签名
        

        $_reqeust = array(
            'merId' => '898000000020186', 					//商户号
            'tradeCode' => '500302',  				//通道代号
            'orderId' => $orderId, 			    //订单号
            'msg' => $msg,
            'backUrl' => $notifyurl,
        );
        $date='';
	foreach ($_reqeust as $key => $val) {
            $date = $date . $key . "=" . $val . "&";
        }
        $date=$date.'signature='.$signature;
//        echo $date;exit;
        $tuCurl = curl_init();   
        curl_setopt($tuCurl, CURLOPT_URL, $gateway);   
        curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);  
        curl_setopt($tuCurl, CURLOPT_HEADER, 0);    
        curl_setopt($tuCurl, CURLOPT_SSLCERT,$key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD,"123pay");
        curl_setopt($tuCurl, CURLOPT_SSLCERTTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_SSLKEY, $key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLKEYPASSWD,"123pay");  
        curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE,"PEM");        
//        curl_setopt($tuCurl, CURLOPT_CAINFO, $cert_file);         
        curl_setopt($tuCurl, CURLOPT_POST, 1);   
        curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false);   
        curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $date);   
        $tuData = curl_exec($tuCurl);    
        if(!curl_errno($tuCurl)){   
        $info = curl_getinfo($tuCurl);   
                //echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];   
        } else {   
                echo 'Curl error: ' . curl_error($tuCurl);   
        }   
        curl_close($tuCurl);   
        echo $tuData;
    }
    
    //单笔T0代付/暂时只支持个人
    function singleT0Payment4Baifu(){
        $notifyurl = 'http://jhpay.yizhibank.com/baifuback'; //异步通知
        $gateway = 'http://tech.eidpay.com:8080/juPay';        //提交地址
        $code = '500303';
        $money = 100;       //单位分
        $key_file = storage_path('key/898000000020186_public.pem');     //密钥
        $bankcard = '6228480038395977176';
        $orderId = date('YmdHis'). mt_rand(11, 99);
        $name = '中宏测试';
        $str = date('YmdHis').'|02|'.$bankcard.'|'.$name.'||||中国建设银行|'.str_pad($money,15,0,STR_PAD_LEFT).'|111111111111111|111111111111|资金用途';
        $msg = $this->encrypt($str,$key_file,1,'123pay'); //对str进行加密
//	$sha1_str=sha1($str);
        $signature_str='898000000020186'.$orderId.$code.$msg; //商户编号+订单号+代号+msg
        $signature = $this->juSignature($signature_str,$key_file);  //签名
        
        $_reqeust = array(
            'merId' => '898000000020186', 					//商户号
            'tradeCode' => $code,  			//通道代号
            'orderId' => $orderId, 			    //订单号
            'msg' => $msg,
            'backUrl' => $notifyurl,
        );
        $date='';
	foreach ($_reqeust as $key => $val) {
            $date = $date . $key . "=" . $val . "&";
        }
        $date=$date.'signature='.$signature;
//        echo $date;exit;
        $tuCurl = curl_init();   
        curl_setopt($tuCurl, CURLOPT_URL, $gateway);   
        curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);  
        curl_setopt($tuCurl, CURLOPT_HEADER, 0);    
        curl_setopt($tuCurl, CURLOPT_SSLCERT,$key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD,"123pay");
        curl_setopt($tuCurl, CURLOPT_SSLCERTTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_SSLKEY, $key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLKEYPASSWD,"123pay");  
        curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE,"PEM");        
//        curl_setopt($tuCurl, CURLOPT_CAINFO, $cert_file);         
        curl_setopt($tuCurl, CURLOPT_POST, 1);   
        curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false);   
        curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $date);   
        $tuData = curl_exec($tuCurl);    
        if(!curl_errno($tuCurl)){   
        $info = curl_getinfo($tuCurl);   
                //echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];   
        } else {   
                echo 'Curl error: ' . curl_error($tuCurl);   
        }   
        curl_close($tuCurl);   
        echo $tuData;
    }
    
    //代付交易结果查询
    function searchResult4Single(){
        $notifyurl = 'http://jhpay.yizhibank.com/baifuback'; //异步通知
        $gateway = 'http://tech.eidpay.com:8080/juPay';        //提交地址
        $code = '500501';
        $precode = '500302';
        $key_file = storage_path('key/898000000020186_public.pem');
        $orderId = date('YmdHis'). mt_rand(11, 99);
        $str = date('YmdHis').'|'.$orderId.'|'.$precode;
        $msg = $this->encrypt($str,$key_file,1,'123pay'); //对str进行加密
//	$sha1_str=sha1($str);
        $signature_str='898000000020186'.$orderId.$code.$msg; //商户编号+订单号+代号+msg
        $signature = $this->juSignature($signature_str,$key_file);  //签名
        
        $_reqeust = array(
            'merId' => '898000000020186', 					//商户号
            'tradeCode' => $code,  			//通道代号
            'orderId' => $orderId, 			    //订单号
            'msg' => $msg,
            'backUrl' => $notifyurl,
        );
        $date='';
	foreach ($_reqeust as $key => $val) {
            $date = $date . $key . "=" . $val . "&";
        }
        $date=$date.'signature='.$signature;
//        echo $date;exit;
        $tuCurl = curl_init();   
        curl_setopt($tuCurl, CURLOPT_URL, $gateway);   
        curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);  
        curl_setopt($tuCurl, CURLOPT_HEADER, 0);    
        curl_setopt($tuCurl, CURLOPT_SSLCERT,$key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD,"123pay");
        curl_setopt($tuCurl, CURLOPT_SSLCERTTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_SSLKEY, $key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLKEYPASSWD,"123pay");  
        curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE,"PEM");        
//        curl_setopt($tuCurl, CURLOPT_CAINFO, $cert_file);         
        curl_setopt($tuCurl, CURLOPT_POST, 1);   
        curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false);   
        curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $date);   
        $tuData = curl_exec($tuCurl);    
        if(!curl_errno($tuCurl)){   
        $info = curl_getinfo($tuCurl);   
                //echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];   
        } else {   
                echo 'Curl error: ' . curl_error($tuCurl);   
        }   
        curl_close($tuCurl);   
        echo $tuData;
    }
    
    //账户余额查询
    function searchBalance(){
        $notifyurl = 'http://jhpay.yizhibank.com/baifuback'; //异步通知
        $gateway = 'http://tech.eidpay.com:8080/juPay';        //提交地址
        $code = '500401';
        $type = '01';   //01-代付基本账户；02-代付T0账户
        $key_file = storage_path('key/898000000020186_public.pem');
        $orderId = date('YmdHis'). mt_rand(11, 99);
        $str = date('YmdHis').'|'.$type;
        $msg = $this->encrypt($str,$key_file,1,'123pay'); //对str进行加密
//	$sha1_str=sha1($str);
        $signature_str='898000000020186'.$orderId.$code.$msg; //商户编号+订单号+代号+msg
        $signature = $this->juSignature($signature_str,$key_file);  //签名
        
        $_reqeust = array(
            'merId' => '898000000020186', 					//商户号
            'tradeCode' => $code,  			//通道代号
            'orderId' => $orderId, 			    //订单号
            'msg' => $msg,
            'backUrl' => $notifyurl,
        );
        $date='';
	foreach ($_reqeust as $key => $val) {
            $date = $date . $key . "=" . $val . "&";
        }
        $date=$date.'signature='.$signature;
//        echo $date;exit;
        $tuCurl = curl_init();   
        curl_setopt($tuCurl, CURLOPT_URL, $gateway);   
        curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);  
        curl_setopt($tuCurl, CURLOPT_HEADER, 0);    
        curl_setopt($tuCurl, CURLOPT_SSLCERT,$key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD,"123pay");
        curl_setopt($tuCurl, CURLOPT_SSLCERTTYPE,"PEM");        
        curl_setopt($tuCurl, CURLOPT_SSLKEY, $key_file);   
        curl_setopt($tuCurl, CURLOPT_SSLKEYPASSWD,"123pay");  
        curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE,"PEM");        
//        curl_setopt($tuCurl, CURLOPT_CAINFO, $cert_file);         
        curl_setopt($tuCurl, CURLOPT_POST, 1);   
        curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false);   
        curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $date);   
        $tuData = curl_exec($tuCurl);    
        if(!curl_errno($tuCurl)){   
        $info = curl_getinfo($tuCurl);   
                //echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];   
        } else {   
                echo 'Curl error: ' . curl_error($tuCurl);   
        }   
        curl_close($tuCurl);   
        echo $tuData;
    }
   
    private function object_array($array) {  
            if(is_object($array)) {  
                    $array = (array)$array;  
            } if(is_array($array)) {  
                    foreach($array as $key=>$value) {  
                            $array[$key] = $this->object_array($value);  
                            }  
            }  
            return $array;  
    }	
    //点对点通知
    public function notifyurl()
    {
        $streamData = isset($GLOBALS['HTTP_RAW_POST_DATA'])? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
        if(empty($streamData)){
            $streamData = file_get_contents('php://input');
        }
        $data=json_decode($streamData);
        $data=$this->object_array($data);
        $key_file = "/home/www/cert/商户号_public.pem";
        $sign=$this->juValidateSignature($streamData,$key_file);
        if($sign){
            if($data["ordStatus"]=='00000'){
                    echo "ok";
            }
        }
    }	
    
    // 生成密码（32位长度的随机字符串）
    private function generate_password( $length = 32 )
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        $max = count($chars)-1;

        // 在 $chars 中随机取 $length 个数组元素键名
        //    $keys = array_rand($chars, $length);
        $password = '';
        for($i = 0; $i < $length; $i++)
        {
            // 将 $length 个数组元素连接成字符串
            //        $password .= $chars[$keys[$i]];
            $password .= $chars[rand(0,$max)];
        }

        return $password;
    }

    // DES加密
    private function encrypt_with_des($data, $key)
    {
        if ($data == null || $key == null)
        {
            return '';
        }

        $size_key = mcrypt_get_key_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_ECB);
        $size_iv = mcrypt_get_iv_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_ECB);
        $size_block = mcrypt_get_block_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_ECB);
        if (strlen($key) < $size_key)
        {
            return '';
        }
        $key = substr($key, 0, $size_key);

        $iv = $key;

        $pad = $size_block - (strlen($data) % $size_block);
        $data .= str_repeat(chr($pad), $pad);
        $encrypted_data = mcrypt_encrypt(MCRYPT_TRIPLEDES, $key, $data, MCRYPT_MODE_ECB, $iv);
        return $encrypted_data;
    }

    // DES解密
    private function decrypt_with_des($data, $key)
    {
        if ($data == null || $key == null)
        {
            return '';
        }

        $size_key = mcrypt_get_key_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_ECB);
        $size_iv = mcrypt_get_iv_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_ECB);
        if (strlen($key) < $size_key)
        {
            return '';
        }
        $key = substr($key, 0, $size_key);

        $iv = $key;

        $decrypted_data = mcrypt_decrypt(MCRYPT_TRIPLEDES, $key, $data, MCRYPT_MODE_ECB, $iv);
        $pad = ord($decrypted_data{strlen($decrypted_data) - 1});
        return substr($decrypted_data, 0, -1 * $pad);
    }

    // 加密（商户接入需调用此接口）
    private function encrypt($data, $key_file, $is_pubkey = true, $pwd_prikey = "")
    {
        if ($data == null || $key_file == null)
        {
            return '';
        }

        if (! file_exists($key_file))
        {
            return '';
        }

        $content_key = file_get_contents($key_file);
        $key = null;
        if ($is_pubkey)
        {
            $key = openssl_pkey_get_public($content_key);
        } else {
            $key = openssl_pkey_get_private($content_key, $pwd_prikey);
        }
        if ($key == null)
        {
            return '';
        }
        $password = $this->generate_password();
        $result2 = bin2hex($this->encrypt_with_des($data, $password));
        $result1 = '';

        $size_block = 1024 / 8;
        $pad = $size_block - (strlen($password) % $size_block);
        $password = str_repeat(chr(0), $pad) . $password;

        if ($is_pubkey)
        {
            openssl_public_encrypt($password, $result1, $key, OPENSSL_NO_PADDING);
        } else {
            openssl_private_encrypt($password, $result1, $key, OPENSSL_NO_PADDING);
        }
        openssl_free_key($key);
        $result1 = bin2hex($result1);
        return ($result1 . $result2);
    }

    // 解密
    private function decrypt($data, $key_file, $is_pubkey = false, $pwd_prikey = "")
    {
        if ($data == null || $key_file == null)
        {
            return '';
        }

        $len = strlen($data);
        if ($len <= 256)
        {
            return '';
        }

        if (! file_exists($key_file))
        {
            return '';
        }

        $content_key = file_get_contents($key_file);
        $key = null;
        if ($is_pubkey)
        {
            $key = openssl_pkey_get_public($content_key);
        } else {
            $key = openssl_pkey_get_private($content_key, $pwd_prikey);
        }
        if ($key == null)
        {
            return '';
        }

        $result1 = substr($data, 0, 256);
        $result2 = substr($data, 256, $len-256);
        $result1 = hex2bin($result1);
        $result2 = hex2bin($result2);
        $password = '';

        if ($is_pubkey)
        {
            openssl_public_decrypt($result1, $password, $key, OPENSSL_NO_PADDING);
        } else {
            openssl_private_decrypt($result1, $password, $key, OPENSSL_NO_PADDING);
        }
        openssl_free_key($key);
        if ($password == null)
        {
            return '';
        }

        $password = trim($password);
        return $this->decrypt_with_des($result2, $password);
    }
	
    // 签名（商户接入需调用此接口）
    private function juSignature($info, $key_file)
    {
        if ($info == null || $key_file == null)
        {
            return '';
        }

        if (! file_exists($key_file))
        {
            return '';
        }

        $strKeyData = '';
        $handle = @fopen($key_file, "r");
        if ($handle == null)
        {
            return '';
        }
        while (! feof($handle))
        {
            $strLine = fgets($handle, 8192);
            $strLine = trim($strLine);
            if ( $strLine == null || strlen($strLine) == 0 ||
                strpos($strLine, '--') !== false )
            {
                continue;
            }
            $strKeyData .= $strLine;
        }
        fclose($handle);

        return md5($strKeyData . $info);
    }

    // 验签（商户接入需调用此接口）
    private function juValidateSignature($info, $key_file)
    {
        if ($info == null || $key_file == null)
        {
            return false;
        }
        if (! file_exists($key_file))
        {
            return false;
        }
        $nSignStart = stripos($info, '"signature":"');
        $nSignEnd = stripos($info, '"', $nSignStart+13);
        $strSigned = substr($info, $nSignStart+13, $nSignEnd-$nSignStart-13);
        $info = str_replace($strSigned, '@eidpay', $info);
        $strValidate = $this->juSignature($info, $key_file);
        return strcasecmp($strSigned, $strValidate) == 0;
    }
    
    //扫码签名
    private function sign($data) {
        $certs = array();
        openssl_pkcs12_read(file_get_contents(storage_path("key/999002100009999.pfx")), $certs, "123pay"); 
//        dd($certs);
        if(!$certs) return ;
        $signature = '';  
        openssl_sign($data, $signature, $certs['pkey']);
        return base64_encode($signature); 
    }
}
